/**
 * #############################################################################
 * #
 * 
 * DOCOMO Innovations, Inc.
 * 
 * Copyright © 2011 DOCOMO Innovations, Inc. All rights reserved.
 * 
 * 
 * 
 * Date of Preparation: 08/01/2011
 * 
 * Contact: kozat@docomoinnovations.com
 * 
 * 
 * 
 * All intellectual property rights related to this software are vested in
 * DOCOMO Innovations.
 * 
 * DOCOMO Innovations is under no obligation to maintain or support the
 * software.
 * 
 * Neither DOCOMO Innovations nor its employees are responsible for any damage
 * or loss resulting from the use of the software.
 * 
 * #############################################################################
 * #
 */

package umich.infinity.proxy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import umich.infinity.thrift.Aggregator;
import umich.infinity.thrift.Attr;
import umich.infinity.thrift.DirEntry;
import umich.infinity.thrift.UserToken;
import umich.infinity.thrift.Aggregator.Client;


public class ThriftClient {
	public static void main(String[] args) {
		try {
			//TTransport transport = new TSocket("ec2-184-72-12-206.us-west-1.compute.amazonaws.com", 9000);
			TTransport transport = new TSocket("localhost", 9999);
			TProtocol protocol = new TBinaryProtocol(transport);
			Aggregator.Client client = new Aggregator.Client(protocol);

			transport.open();
			
			System.out.println ("Sucessfully login");
			/*
			
			File dir = new File("/home/stgyd/InfinityProxy");

			File[] list = dir.listFiles();

			List<DirEntry> ret = new ArrayList<DirEntry>();

			for (int i = 0; i < list.length; i++) {
				if (list[i].exists() && list[i].isFile()) {
					DirEntry de = new DirEntry();
					de.attr = new Attr();
					de.name = list[i].getName();
					de.attr.size = list[i].length();
					de.attr.priority = 0;
					ret.add(de);
				}
			}
			
			File f = new File(Para.CacheRoot + token.uid + ".noti");
			
			FileOutputStream fos = new FileOutputStream(f);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(ret);

			oos.close();*/
		
			String yhGuoId = "100002572131516";//"100002572131516"; // Yihua Guo
			String yhGuoAcTok = "AAACEdEose0cBAJqpM0llk1XQFwX3wwt8PZArq7pu8zy9ZAZCLRdMveCpzxMgWcAzx2W09UK2GlDiZC4uTxBZBjvD825EOhL7DgTc6xlBqfJct4d6SS8XI";
			String ydGaoId = "1336341331"; // Yudong Gao
			String ydGaoAcTok = "145634995501895|1a94eae701d26809043e1dbc.1-1336341331|SJdJshNkv5tBRaarnqFLwkr1ge4";
			
			UserToken ut = new UserToken();
			ut.uid = yhGuoId;
			
			System.out.println("Starting first login");
			client.backend_login(yhGuoId, yhGuoAcTok);
			System.out.println("After first login");
			
			/*
			
			File f = new File("/home/stgyd/1537606283539.jpg");
			
			String id = client.backend_new_write(ut, f.length());
			
			System.out.println("Id: " + id);
			
			FileInputStream fis;
			try {
				fis = new FileInputStream(f);

				BufferedInputStream bis = new BufferedInputStream(fis);

				byte[] tmp = new byte[1024];

				int i=0;
				while (true) {
					int r = bis.read(tmp);
					if (r == -1) break;
					ByteBuffer buf = ByteBuffer.wrap(tmp); 
					client.backend_write(ut, id, buf, i*1024, 1024);
					i++;
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			/*
			byte[] tmp = new byte[1024];
			for (int i=0;i<1024;i++) {
				tmp[i] = 65;
			}
			
			ByteBuffer buf = ByteBuffer.wrap(tmp); 
			
			for (int i=0;i<15;i++) {
				client.backend_write(ut, id, buf, i*1024, 1024);
			}
			
			buf = client.backend_read(null, "1329982773081.jpg" , 0, 0);
			
			System.out.println(buf.capacity());
			*/
			
			
			
			/*
			Thread.sleep(5000);
			
			System.out.println("Starting second login");
			client.backend_login("1336341331", "145634995501895|1a94eae701d26809043e1dbc.1-1336341331|SJdJshNkv5tBRaarnqFLwkr1ge4");

			System.out.println("After second login");
			
			Thread.sleep(5000);
			
			System.out.println("Starting first logout");
			
			client.backend_logout("1336341331", "145634995501895|1a94eae701d26809043e1dbc.1-1336341331|SJdJshNkv5tBRaarnqFLwkr1ge4");
			
			System.out.println("After first logout");
			
			//client.backend_register("100002394671425", "193051640748441|13a1e0b87ceeed68f9294499.3-100002394671425|Bz5feKz_t42jqJ9a-FyW_yO7GK8");
			
			return;
			
			/*
			client.upload_token("145634995501895|2.AQAAssleFHiqWrKk.3600.1312498800.1-1336341331|O8RMMWIXmOvKQ8lJScYGKMd8FF8");
			List<FriendEntry> lfe = client.read_friendlist("me");
			System.out.println(lfe.get(16).name);
			List<AlbumEntry> lae = client.read_albumlist(lfe.get(16).id);
			List<PhotoEntry> lpe = client.read_album("129952997094522");
			*/
			/*
			ByteBuffer buffer = client.get_photo(lpe.get(0).id);
			
			FileOutputStream file = new FileOutputStream("/home/stgyd/"+"client.jpg");

			BufferedOutputStream out = new BufferedOutputStream(file);
			out.write(buffer.array());

			out.flush();
			out.close();*/
			
		} catch (TException x) {
			System.out.println("Connection dropped");
			
			x.printStackTrace();
		}
	}
}